package com.monkeyboy;

import com.monkeyboy.dao.BaseDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest1 {
    @Autowired
    BaseDao bd;
    @Autowired
    private TestService testService;

    @Test
    public void addCourse() throws Exception{
//        for (int i = 0; i < 5; i++) {
//            Map<String,Object> c = new HashMap<>();
//            c.put("cname", "sharding: "+i);
//            c.put("user_id", Long.valueOf("" + (1000 + i)));
//            c.put("cstatus", "1");
//            bd.save("UserMapper.insertOneCourse", c);
//        }
        Map<String,Object> c = new HashMap<>();
            c.put("c_id", 654111281296441344L);
        bd.save("UserMapper.insertOneCourse", c);
    }

    @Test
    public void test1() throws Exception {

        for (int i = 0; i < 10; i++) {
            Map<String, Object> preUser = new HashMap<>();
            preUser.put("user_id", UUID.randomUUID().toString());
            preUser.put("username", "姓名：" + i);
            preUser.put("status", i % 2);
            bd.save("UserMapper.insertOneUser", preUser);
        }
    }

    @Test
    public void standard() throws Exception {
        Map<String,String> table= new HashMap<>();
        table.put("647191731548192768","course_2");
        MyThreadLocal.myThreadLocal.set(table);
        Map<String, Object> pd = new HashMap<>();
        pd.put("query", "cid between 1111111111111111 and 647018024334786560");
        pd.put("cid",647191731548192768L);
        List<Map<String, Object>> forList = (List<Map<String, Object>>) bd.findForList("UserMapper.selectCourseByQueryStatement2", pd);
        System.out.println("---------------");
        forList.forEach(System.out::println);
        System.out.println("--------------");
    }
    @Test
    public void complex() throws Exception {
        Map<String, Object> pd = new HashMap<>();
        pd.put("query", "cid between 647018024116682753 and 647018024355758081");
        pd.put("cid",647191731548192768L);
        List<Map<String, Object>> forList = (List<Map<String, Object>>) bd.findForList("UserMapper.selectCourseByQueryStatement2", pd);
        System.out.println("---------------");
        System.out.println(testService);
        forList.forEach(System.out::println);
        System.out.println("--------------");
    }
    @Test
    public void hint() throws Exception {
//        HintManager hintManager = HintManager.getInstance();
//        hintManager.addTableShardingValue("course","1");
        List<Map<String, Object>> forList = (List<Map<String, Object>>) bd.findForList("UserMapper.selectCourseByQueryStatement2", null);
        forList.forEach(System.out::println);
    }

    @Test
    public void broadcast()throws Exception{
        Map<String,Object> d1=new HashMap<>();
        d1.put("ustatus","1");
        d1.put("uvalue","正常");
        bd.save("UserMapper.insertOneDict",d1);

        Map<String,Object> d2=new HashMap<>();
        d2.put("ustatus","0");
        d2.put("uvalue","不正常");
        bd.save("UserMapper.insertOneDict",d2);
    }

    @Test
    public void binding()throws Exception{
        for(int i = 0 ; i < 10 ; i ++){
            Map<String,Object> user=new HashMap<>();
            user.put("username","user No "+i);
            user.put("ustatus",(i%2));
            user.put("uage",(i*10));
            bd.save("UserMapper.insertOneUser",user);
        }
    }
    @Test
    public void bindingSelect()throws Exception{
        List<Map<String,Object>> result= (List<Map<String, Object>>) bd.findForList("UserMapper.selectBinding",null);
        System.out.println(result.size());
    }
    @Test
    public void selectCourse()throws Exception{
        MyThreadLocal.myThreadLocal.set(1);
        Map<String,Object> pd=new HashMap<>();
        pd.put("cid",647058713194004480L);
        List<Map<String,Object>> result= (List<Map<String, Object>>) bd.findForList("UserMapper.selectCourseByQueryStatement2",pd);
        result.forEach(System.out::println);
        System.out.println(MyThreadLocal.myThreadLocal.get());
    }
    @Test
    public void selectTest()throws Exception{
        List<Map<String,Object>> result= (List<Map<String, Object>>) bd.findForList("UserMapper.selectTestByQueryStatement",null);
        System.out.println("-----------------");
        result.forEach(System.out::println);
    }
}
